package com.sansecy.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;

public class SimpleFrameTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
//                SimpleFrame frame = new SimpleFrame();
//                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//                frame.setLocationByPlatform(true);
//                frame.setVisible(true);
//                frame.setTitle("SizeFrame");
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                int width = screenSize.width / 2;
                int height = screenSize.height / 2;

////                frame.setExtendedState(Frame.MAXIMIZED_BOTH);
//                ImageIcon imageIcon = new ImageIcon("ic_rocket.png");
//                frame.setIconImage(imageIcon.getImage());
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ImageIcon imageIcon = new ImageIcon("/Users/sansecy/IdeaProjects/TestProject/ic_rocket.png");
                frame.setIconImage(imageIcon.getImage());
                frame.setVisible(true);
                frame.setBounds(width / 2, height / 2, width, height);
                frame.add(new NotHelloWorldComponent());
                frame.add(new imageComponent());
            }
        });


//        String[] familyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
//        for (String s : familyNames) {
//            System.out.println(s);
//        }
    }

    static class SimpleFrame extends JFrame {

        public SimpleFrame() {
//            setSize(300, 200);
        }
    }

    static class NotHelloWorldComponent extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Font sana = new Font("Sana", Font.BOLD, 14);
            setSize(500, 500);
            g.drawString("Not a Hello World program", 75, 100);
            g.drawRect(0, 0, 100, 150);
            g.setColor(Color.blue);
            g.fillRect(100, 150, 100, 150);
            g.setColor(Color.RED);
            Graphics2D gg = (Graphics2D) g;
            g.drawString("Not a Hello World program", 175, 300);
            gg.setPaint(Color.GREEN);
            g.setFont(sana);
            g.drawString("Not a Hello World program", 275, 400);

//            Color brighter = Color.GREEN.brighter().brighter().brighter();
            setBackground(SystemColor.window);

            //从网络创建字体
//            try {
//                URL url = new URL("http://www.fonts.com/Wingbats.ttf");
//                InputStream stream = url.openStream();
//                Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
//            } catch (FontFormatException | IOException e) {
//                e.printStackTrace();
//            }

            String string = "Not a Hello World program";
            FontRenderContext context = ((Graphics2D) g).getFontRenderContext();
            Font font = g.getFont();
            Rectangle2D bounds = font.getStringBounds(string, context);
            int width = (int) bounds.getWidth();
            System.out.println("width " + width);
            int height = (int) bounds.getHeight();
            System.out.println("height " + height);
            System.out.println("y " + bounds.getY());
            LineMetrics lineMetrics = font.getLineMetrics(string, context);
            System.out.println("ascent " + Math.abs(lineMetrics.getAscent()));
            System.out.println("descent " + Math.abs(lineMetrics.getDescent()));
            System.out.println("leading " + Math.abs(lineMetrics.getLeading()));
            System.out.println("centerX " + bounds.getCenterX());
            System.out.println("centerY " + bounds.getCenterY());

            int x = (int) ((getWidth() - width) / 2);
            int y = (int) (getHeight() - height) / 2;
            double ascent = -bounds.getY();
            int baseLine = getHeight() / 2;
            g.drawString(string, x, (int) (baseLine + lineMetrics.getAscent() / 2));
            g.drawLine(0, baseLine, getWidth(), baseLine);

            g.drawRect(x, y, width, height);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(10, 1);
        }
    }

    static class imageComponent extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            ImageIcon imageIcon = new ImageIcon("/Users/sansecy/IdeaProjects/TestProject/ic_rocket.png");
            int iconWidth = imageIcon.getIconWidth();
            int iconHeight = imageIcon.getIconHeight();
            for (int i = 0; i < getWidth(); i += iconWidth) {
                for (int j = 0; j < getHeight(); j += iconHeight) {
                    g.drawImage(imageIcon.getImage(), i, j, null);
                }
            }
            g.drawImage(imageIcon.getImage(), 0, 0, null);

//            g.drawImage(imageIcon.getImage(), 0, 0, new ImageObserver() {
//                @Override
//                public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
//                    System.out.println("imageUpdate() called with: img = [" + img + "], infoflags = [" + infoflags + "], x = [" + x + "], y = [" + y + "], width = [" + width + "], height = [" + height + "]");
//                    return false;
//                }
//            });
        }
    }
}
